<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备型号" prop="devicemodel">
        <el-input
          v-model="queryParams.devicemodel"
          placeholder="请输入设备型号"
          clearable
          size="large"
          @keyup.enter.native="handleQuery"
        />
        </el-form-item>
      <el-form-item label="起始日期">
        <el-date-picker
          v-model="daterangeStartdate"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['zyx:check:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['zyx:check:edit']"
        >修改</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['zyx:check:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['zyx:check:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="checkList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
 <el-table-column label="单位名称" align="center" prop="zyxDevice.unitname" />
       <el-table-column label="设备类别" align="center" prop="zyxDevice.devicetype" />
      <el-table-column label="设备型号" align="center" prop="zyxDevice.devicemodel" />
      <el-table-column label="起始日期" align="center" prop="startdate" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startdate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="下次提醒日期" align="center" prop="nextdate" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.nextdate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="检定周期" align="center" prop="warncycle" />
      <el-table-column label="备注信息" align="center" prop="remark" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['zyx:check:edit']"
          >修改</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['zyx:check:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改检定基础信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="起始日期" prop="startdate">
          <el-date-picker clearable size="small"
            v-model="form.startdate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择起始日期">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="检定周期" prop="warncycle">
          <el-input v-model="form.warncycle" placeholder="请输入检定周期" @input="changeInput"  maxlength="3">
            <template slot="append">天</template>
            </el-input>
        </el-form-item>
        <el-form-item label="下次检定日期" prop="nextcheckdate" >
          <el-date-picker clearable size="small" disabled="disabled"
            v-model="form.nextcheckdate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="提前" prop="warndaynum">
          <el-input v-model="form.warndaynum"  placeholder="请输入天数" @input="changeInput" maxlength="3">
             <template slot="append">天提醒</template>
            </el-input>
        </el-form-item>
        <el-form-item label="下次提醒日期" prop="nextdate" >
          <el-date-picker clearable size="small" disabled="disabled"
            v-model="form.nextdate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注信息" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" maxlength="500"/>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm"   v-preventReClick="3000">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCheck, getCheck, delCheck, addCheck, updateCheck, exportCheck } from "@/api/zyx/check";

export default {
  name: "Check",
  components: {
  },
  data() {
    return {
      devicetypeOptions:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 检定基础信息表格数据
      checkList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 起始日期时间范围
      daterangeStartdate: [],
      // 下次日期时间范围
      daterangeNextdate: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceid: null,
        startdate: null,
        devicemodel:null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        startdate: [
          { required: true, message: "起始日期不能为空", trigger: "blur" }
        ],
        nextcheckdate:[
           { required: true, message: "下次检定日期不能为空", trigger: "blur" }
        ],
        nextdate: [
          { required: true, message: "下次日期不能为空", trigger: "blur" }
        ],
        warncycle: [
          { required: true, message: "检定周期不能为空", trigger: "blur" },
          {
            pattern: /^[1-9][0-9]*$/,
            message: "请输入正确的检定周期",
            trigger: "blur"
          }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("zyx_devicetype").then(response => {
      this.devicetypeOptions = response.data;
    });
  },
  methods: {
    formatDate  (value) {
      let date = new Date(value);
      let y = date.getFullYear();
      let MM = date.getMonth() + 1;
      MM = MM < 10 ? ('0' + MM) : MM;
      let d = date.getDate();
      d = d < 10 ? ('0' + d) : d;
      let h = date.getHours();
      h = h < 10 ? ('0' + h) : h;
      let m = date.getMinutes();
      m = m < 10 ? ('0' + m) : m;
      let s = date.getSeconds();
      s = s < 10 ? ('0' + s) : s;
      return y + '-' + MM + '-'  + d + ' '  + h + ':'  + m + ':'  + s;
    },
    //提醒周期变化计算提醒日期
    changeInput() {
      let date2 =new Date(this.form.startdate)
      //计算下次检定日期
      let dateTempnext1 = date2.setDate(date2.getDate() + parseInt(this.form.warncycle) )
      this.form.nextcheckdate=this.formatDate(dateTempnext1)

        let date1 =new Date(this.form.startdate)
      //计算下次提醒日期
         let dateTemp = date1.setDate(date1.getDate() + parseInt(this.form.warncycle)-parseInt(this.form.warndaynum) )
         // console.log(dateTemp)
         this.form.nextdate=this.formatDate(dateTemp)
        },
    // 设备类别字典字典翻译
    devicetypeFormat(row, column) {
      return this.selectDictLabel(this.devicetypeOptions, row.zyxDevice.devicetype);
    },
    /** 查询检定基础信息列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeStartdate && '' != this.daterangeStartdate) {
        this.queryParams.params["beginStartdate"] = this.daterangeStartdate[0];
        this.queryParams.params["endStartdate"] = this.daterangeStartdate[1];
      }
      if (null != this.daterangeNextdate && '' != this.daterangeNextdate) {
        this.queryParams.params["beginNextdate"] = this.daterangeNextdate[0];
        this.queryParams.params["endNextdate"] = this.daterangeNextdate[1];
      }
      listCheck(this.queryParams).then(response => {
        this.checkList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        departid: null,
        deviceid: null,
        startdate: null,
        nextdate: null,
        warncycle: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null,
        nextcheckdate:null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeStartdate = [];
      this.daterangeNextdate = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加检定基础信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCheck(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改检定基础信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          //判断周期和提前提醒的大小
            if(parseInt(this.form.warncycle)<=parseInt(this.form.warndaynum)){
              this.$notify({
              title: '警告',
              message: '检定周期必须大于提前提醒的天数',
              type: 'warning'
            });
            return;
            }
          if (this.form.id != null) {
            updateCheck(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCheck(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除检定信息?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCheck(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有检定基础信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportCheck(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
